package com.waiting.serializable;

import java.io.*;

/**
 * @author bjweitingting
 * @description
 * @date 2018/8/23
 **/
public class ExternalizableTest implements Externalizable {

    private transient String content = "是的，我将会被序列化，不管我是否被transient关键字修饰";
    private String param;

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(content);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {
        content = (String) in.readObject();
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public static void main(String[] args) throws Exception {

        ExternalizableTest et = new ExternalizableTest();
        et.setParam("hello");
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
                new File("test")));
        out.writeObject(et);

        ExternalizableTest etTwo = new ExternalizableTest();
        ObjectInput in = new ObjectInputStream(new FileInputStream(new File(
                "test")));
        etTwo = (ExternalizableTest) in.readObject();

        System.out.println(etTwo.content);
        System.out.println(etTwo.param);
        out.close();
        in.close();
    }
}